package logic

import (
	"context"
	"housing/models/mysql"

	"housing/houserpc/houserpc"
	"housing/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PreListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPreListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PreListLogic {
	return &PreListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PreListLogic) PreList(in *houserpc.PreListRequest) (*houserpc.PreListResponse, error) {
	// todo: add your logic here and delete this line

	var pre []mysql.Pre

	var preList []*houserpc.PreList
	tx := mysql.DB.Debug()

	if in.TimeOrder == 1 {
		tx = tx.Order("pre_time desc")
	} else {
		tx = tx.Order("pre_time asc")
	}

	tx.Find(&pre)

	for _, m := range pre {

		list := houserpc.PreList{
			PreId:   int64(m.ID),
			UserId:  m.UserId,
			HouseId: m.HouseId,
			PreTime: m.PreTime,
		}
		preList = append(preList, &list)
	}

	return &houserpc.PreListResponse{
		List: preList,
	}, nil
}
